import 'package:boss/models/package_group.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/provider/package_detail_provider.dart';
import 'package:boss/ui/page/packageGroup/widget/package_group_item.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/sortList/sort_item_data.dart';
import 'package:boss/ui/widget/sortList/sort_util.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class PackageGroupList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var provider = Provider.of<PackageDetailProvider>(context);
    var packageGroups = provider.package.packagesGroupList;

    if (packageGroups.length == 0) {
      return SizedBox();
    }

    List<Widget> children = [];

    packageGroups.forEach((item) {
      children.add(PackageGroupItem(
        data: item,
        onDelete: () {
          provider.removePackageGroup(item);
        },
      ));
    });

    children.add(Container(
      height: 80.w,
      alignment: Alignment.centerRight,
      child: LabelButton(
        title: '调整排序',
        onTap: () async {
          var res = await showSortList<PackageGroup>(
            context,
            title: '套餐排序',
            items: packageGroups.map((item) => SortItemData(
              title: item.groupName,
              value: item,
            )).toList(),
          );

          if (res != null) {
            provider.setPackageGroup(res);
          }
        },
      ),
    ));

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: children,
    );
  }
}
